<?php
/**
* @desc 登录表单处理
* @author kevin.wang
* @date 2015-01-01 12:00:00
**/
class OrderForm extends BaseForm
{
	public $goods_sn;
	public $sell_num;
	private $_form_data;
	private $_main_order = array();
	//表单规则
	public function rules()
	{
		return array(
			array('goods_sn, sell_num', 'required', 'message'=>'旅客姓名不能为空')
		);
	}
	//数据校验
	public function validateForm(array $form){
		if(empty($form)){
			$this->addError('error','商品信息不能为空');
			return false;
		}
		foreach($form as $value){
			$this->attributes = $value;
			if(!$this->validate()){
				return false;
			}
		}
		$this->_form_data = $form;
		return true;
	}
	//返回写入订单需要的数据
	public function getOrderData(){
		$return = array();
		if(empty($this->_form_data)){
			return $return;
		}
		//主订单数据
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$this->_main_order['user_id'] = $userInfo['user_id'];
		$this->_main_order['order_date'] = date("Y-m-d H:s:i");
		$this->_main_order['order_expire'] = date("Y-m-d H:s:i", strtotime('+32 hour'));
		$this->_main_order['order_sn'] = $this->getOrderSn();
		$this->_main_order['total_amount'] = 0;
		$travelDate = Yii::app()->request->getParam("date");
		if(!empty($travelDate)){
			$this->_main_order['travel_date'] = $travelDate;
		}
		$goods = new Goods;
		//子订单数据
		foreach($this->_form_data as $key=>$value){
			$goodsDetail = $goods->getGoodsDetail($value['goods_sn']);
			//$value['goods_name'] = $goodsDetail['goods_name'];
			$value['price'] = $goodsDetail['price'];
			$value['total_price'] = $goodsDetail['price']*$value['sell_num'];
			$value['order_sn'] = $this->_main_order['order_sn'];
			$value['order_detail_sn'] = $this->getOrderSn();
			$this->_main_order['total_amount'] += $value['total_price'];
			$return[] = $value;
		}
		return $return;
	}
	//获取主订单数据
	public function mainOrderData(){
		return $this->_main_order;
	}
	//订单成功状态处理
	public function processOrder($status, $orderSn){
		$order = new Order;
		return $order->updateOrderStatus($status, $orderSn);
	}
}
